<?php

/**
 * @author Tu Phan
 * @copyright 2012
 */

class Database {
    
    private $db_server = '';
    private $db_name = '';
    private $db_user = '';
    private $db_pass = '';
    private $db_connect = NULL;
    private $db_select = NULL;
    private $result = NULL;
    
    public function __construct($db_server, $db_name, $db_user, $db_pass) {
        $this->db_server = $db_server;
        $this->db_name = $db_name;
        $this->db_user = $db_user;
        $this->db_pass = $db_pass;
        $this->connect();
        $this->select_db();
    }
    
    public function connect() {
        $db_connect = mysql_connect($this->db_server, $this->db_user, $this->db_pass) or die("Unable connect to database !");
    }
    
    public function select_db() {
        $db_select = mysql_select_db($this->db_name) or die('Unable select database !');
    }
    
    public function query($sql) {
        $this->result = mysql_query($sql);
        return $this->result;
    }
    
    public function fetch_array() {
        if (!empty($this->result)) return mysql_fetch_array($this->result);
        return NULL;
    }
    
    public function get_data() {
        $data = array();
        while ($a = $this->fetch_array()) 
        {
            $data[] = $a;
        }
        return $data;
    }
    
    public function total_rows($table) {
        $sql = "select count(id) from $table";
        $data = $this->fetch_array($this->query($sql));
        return $data[0];
    }
    
};

?>